﻿namespace PMS.Client.FrmBackup
{
    using PMS.BLL;
    using PMS.Client.Controls;
    using PMS.Client.Properties;
    using PMS.Common;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class FrmList : Form
    {
        private DataGridViewTextBoxColumn AddDate;
        private string backupPath = (Application.StartupPath + @"\" + Config.BackupPath);
        private ToolStripButton btnAdd;
        private ToolStripButton btnDelete;
        private ToolStripButton btnOpen;
        private IContainer components = null;
        private UCDataGridView dataGridView1;
        private DataGridViewTextBoxColumn FileName;
        private DataGridViewTextBoxColumn filePath;
        private DataGridViewTextBoxColumn fileSize;
        private Label lbTitle;
        private ToolStrip toolStrip1;

        public FrmList()
        {
            this.InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmAdd add = new FrmAdd();
            add.ShowDialog();
            if (add.DialogResult == DialogResult.OK)
            {
                this.FileListBind();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if ((this.dataGridView1.SelectedRows.Count > 0) && (MessageBox.Show("删除将无法恢复，确定删除备份？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK))
            {
                string path = this.dataGridView1.SelectedRows[0].Cells["filePath"].Value.ToString();
                try
                {
                    File.Delete(path);
                    this.FileListBind();
                }
                catch (Exception exception)
                {
                    Log.Write(exception.Message);
                    MessageBox.Show(exception.Message);
                }
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if ((this.dataGridView1.SelectedRows.Count > 0) && (MessageBox.Show("恢复装覆盖现在所有数据，恢复前请备好份，确认恢复？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK))
            {
                string backupFile = this.dataGridView1.SelectedRows[0].Cells["filePath"].Value.ToString();
                try
                {
                    if (new BackupBLL().Recovery(backupFile))
                    {
                        SyslogBLL.Write("数据恢复", "数据恢复成功");
                        MessageBox.Show("数据恢复成功！");
                        base.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("数据恢复成功！");
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FileListBind()
        {
            this.dataGridView1.Rows.Clear();
            DirectoryInfo info = new DirectoryInfo(this.backupPath);
            foreach (FileInfo info2 in info.GetFiles())
            {
                this.dataGridView1.Rows.Add(new string[] { this.backupPath + @"\" + info2.Name, info2.Name, (info2.Length / 0x400L) + "kb", info2.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") });
            }
        }

        private void FrmList_Load(object sender, EventArgs e)
        {
            this.FileListBind();
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmList));
            this.lbTitle = new Label();
            this.toolStrip1 = new ToolStrip();
            this.btnAdd = new ToolStripButton();
            this.btnDelete = new ToolStripButton();
            this.btnOpen = new ToolStripButton();
            this.dataGridView1 = new UCDataGridView(this.components);
            this.filePath = new DataGridViewTextBoxColumn();
            this.FileName = new DataGridViewTextBoxColumn();
            this.fileSize = new DataGridViewTextBoxColumn();
            this.AddDate = new DataGridViewTextBoxColumn();
            this.toolStrip1.SuspendLayout();
            ((ISupportInitialize) this.dataGridView1).BeginInit();
            base.SuspendLayout();
            this.lbTitle.BackColor = Color.Gold;
            this.lbTitle.Dock = DockStyle.Top;
            this.lbTitle.Font = new Font("宋体", 14.25f, FontStyle.Bold, GraphicsUnit.Point, 0x86);
            this.lbTitle.ForeColor = Color.Black;
            this.lbTitle.Location = new Point(0, 0);
            this.lbTitle.Name = "lbTitle";
            this.lbTitle.Size = new Size(0x28c, 0x27);
            this.lbTitle.TabIndex = 13;
            this.lbTitle.Text = "备份文件列表";
            this.lbTitle.TextAlign = ContentAlignment.MiddleCenter;
            this.toolStrip1.Items.AddRange(new ToolStripItem[] { this.btnAdd, this.btnDelete, this.btnOpen });
            this.toolStrip1.Location = new Point(0, 0x27);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.RenderMode = ToolStripRenderMode.System;
            this.toolStrip1.Size = new Size(0x28c, 0x38);
            this.toolStrip1.TabIndex = 0x10;
            this.toolStrip1.Text = "toolStrip1";
            this.btnAdd.Image = Resources.Icons_add;
            this.btnAdd.ImageScaling = ToolStripItemImageScaling.None;
            this.btnAdd.ImageTransparentColor = Color.Magenta;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new Size(60, 0x35);
            this.btnAdd.Text = "添加备份";
            this.btnAdd.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnAdd.Click += new EventHandler(this.btnAdd_Click);
            this.btnDelete.Image = Resources.delete;
            this.btnDelete.ImageScaling = ToolStripItemImageScaling.None;
            this.btnDelete.ImageTransparentColor = Color.Magenta;
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new Size(60, 0x35);
            this.btnDelete.Text = "删除备份";
            this.btnDelete.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnDelete.Click += new EventHandler(this.btnDelete_Click);
            this.btnOpen.Image = Resources.view;
            this.btnOpen.ImageScaling = ToolStripItemImageScaling.None;
            this.btnOpen.ImageTransparentColor = Color.Magenta;
            this.btnOpen.Name = "btnOpen";
            this.btnOpen.Size = new Size(60, 0x35);
            this.btnOpen.Text = "备份恢复";
            this.btnOpen.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnOpen.Click += new EventHandler(this.btnOpen_Click);
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            this.dataGridView1.BackgroundColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.dataGridView1.BorderStyle = BorderStyle.None;
            this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dataGridView1.Columns.AddRange(new DataGridViewColumn[] { this.filePath, this.FileName, this.fileSize, this.AddDate });
            style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            style.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            style.Font = new Font("宋体", 9f);
            style.ForeColor = Color.Black;
            style.SelectionBackColor = SystemColors.Highlight;
            style.SelectionForeColor = Color.Black;
            style.WrapMode = DataGridViewTriState.True;
            this.dataGridView1.DefaultCellStyle = style;
            this.dataGridView1.Dock = DockStyle.Fill;
            this.dataGridView1.GridColor = Color.FromArgb(0xd0, 0xd7, 0xe5);
            this.dataGridView1.HighlightSelectedColumnHeaders = false;
            this.dataGridView1.Location = new Point(0, 0x5f);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowTemplate.Height = 0x17;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new Size(0x28c, 0xd3);
            this.dataGridView1.TabIndex = 0x11;
            this.filePath.DataPropertyName = "FilePath";
            this.filePath.HeaderText = "FilePath";
            this.filePath.Name = "filePath";
            this.filePath.ReadOnly = true;
            this.filePath.Visible = false;
            this.FileName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.FileName.DataPropertyName = "FileName";
            this.FileName.HeaderText = "文件名";
            this.FileName.Name = "FileName";
            this.FileName.ReadOnly = true;
            this.fileSize.HeaderText = "文件大小";
            this.fileSize.Name = "fileSize";
            this.fileSize.ReadOnly = true;
            this.AddDate.DataPropertyName = "AddDate";
            this.AddDate.HeaderText = "备份时间";
            this.AddDate.Name = "AddDate";
            this.AddDate.ReadOnly = true;
            this.AddDate.Width = 150;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x28c, 0x132);
            base.Controls.Add(this.dataGridView1);
            base.Controls.Add(this.toolStrip1);
            base.Controls.Add(this.lbTitle);
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "FrmList";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "系统备份列表";
            base.Load += new EventHandler(this.FrmList_Load);
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            ((ISupportInitialize) this.dataGridView1).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}

